﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Common
{
    public class Module_Page
    {
        private int _PageCount, _PageNum, _PageSize, _AllCount, _LastPageCount, _First,_Max;
        private string _PageName;
        public Module_Page()
        {
            _PageCount = 0;
            _PageNum = 1;
            _PageSize = 10;
            _AllCount = 0;
            _LastPageCount = 0;
            _PageName = "";
            _First = 0;
            _Max = 0;
        }

        public int PageCount
        {
            get { return _PageCount; }
            set { _PageCount = value; }
        }

        public int PageNum
        {
            get { return _PageNum; }
            set { _PageNum = value; }
        }

        public int PageSize
        {
            get { return _PageSize; }
            set { _PageSize = value; }
        }

        public int AllCount
        {
            get { return _AllCount; }
            set { _AllCount = value; }
        }

        public int LastPageCount
        {
            get { return _LastPageCount; }
            set { _LastPageCount = value; }
        }

        public string PageName
        {
            get { return _PageName; }
            set { _PageName = value; }
        }

        public int Max
        {
            get { return _Max; }
            set { _Max = value; }
        }

        public int First
        {
            get { return _First; }
            set { _First = value; }
        }

        public void Compute()
        {
            if (_AllCount == 0)
            {
                _PageCount = 1;
                return;
            }
            if (_AllCount % _PageSize > 0)
            {
                _PageCount = _AllCount / _PageSize + 1;
                _LastPageCount = _AllCount % _PageSize;
            }
            else
            {
                _PageCount = _AllCount / _PageSize;
                _LastPageCount = _PageSize;
            }

            if (_PageNum > _PageCount)
            {
                _PageNum = _PageCount;
            }

            _First = (_PageNum - 1) * _PageSize;
            _Max = PageSize;
            if (_Max > _AllCount)
            {
                _Max = _LastPageCount;
            }
            

            //if (_AllCount == 0 || _PageCount==0)
            //{
            //    _PageCount = 1;
            //    return;
            //}
            //else if (_AllCount % _PageSize > 0)
            //{
            //    _PageCount = _AllCount / _PageSize + 1;
            //    _LastPageCount = _AllCount % _PageSize;
            //}
            //else
            //{
            //    _PageCount = _AllCount / _PageSize;
            //    _LastPageCount = _PageSize;
            //}

            //if (_PageNum > _PageCount)
            //{
            //    _PageNum = _PageCount;
            //}

            ////如果数据总数小于每页数量且页码>1，则返回空，否则返回全部数据作为第一页。
            //if (_AllCount < _PageSize)
            //{
            //        _First = 0;
            //        _Max = AllCount;
            //}
            //else if ((_PageNum * _PageSize) > _AllCount)//如果要取得数据超过了数据范围一页的数量，则返回一个Null，否则返回最后一页数据
            //{
            //    _First = _AllCount - _LastPageCount;
            //    _Max = _LastPageCount;
            //}
            //else//计算分页数据
            //{
            //    _First = (_PageNum - 1) * _PageSize;
            //    _Max = _PageSize;
            //}
        }

        public string GetNumPage()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("分页：");
            for (int i = 1; i <= _PageCount; i++)
            {
                sb.Append(" <a href='");
                sb.Append(_PageName);
                sb.Append(i);
                sb.Append("'>");
                if (i == _PageNum)
                {
                    sb.Append("<span class='.spcolor'>");
                    sb.Append(i);
                    sb.Append("</span>");
                }
                else
                {
                    sb.Append(i);
                }
                sb.Append("</a>");
            }

            return sb.ToString();
        }

        public string GetPage()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("共");
            sb.Append(_AllCount);
            sb.Append("条数据 | 分页：");
            sb.Append(_PageNum);
            sb.Append("/");
            sb.Append(_PageCount);
            sb.Append(" | 页码：");
            if (_PageNum == 1)
            {
                sb.Append("首页 | 上一页 |");
            }
            else
            {
                sb.Append("<a href='");
                sb.Append(_PageName);
                sb.Append("1'>首页</a> | <a href='");
                sb.Append(_PageName);
                sb.Append(_PageNum - 1);
                sb.Append("'>上一页</a> |");
            }

            if (_PageNum == _PageCount)
            {
                sb.Append(" 下一页 | 末页 |");
            }
            else
            {
                sb.Append("<a href='");
                sb.Append(_PageName);
                sb.Append(_PageNum + 1);
                sb.Append("'>下一页</a> | <a href='");
                sb.Append(_PageName);
                sb.Append(_PageCount);
                sb.Append("'>末页</a> |");
            }
            sb.Append(" <a href='javascript:' id='PageGo'>跳转</a>：<input type='text' name='PageCount' id='PageCount' size='3' value='");
            sb.Append(_PageNum);
            sb.Append("' title='设定要跳转的页码' />");
            sb.Append("\r\n<script>\r\n$(function(){$(\"#PageGo\").click(function(){location.href=\"");
            sb.Append(_PageName);
            sb.Append("\"+$(\"#PageCount\").val()});});\r\n</script>\r\n");
            return sb.ToString();
        }
    }
}
